/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.Product;
import entities.ShoppingCart;
import entities.Transactions;
import factory.DBConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author theworldiwant
 */
public abstract class MySQLShoppingCartDAO extends ShoppingCartDAO{
    public void addToCart(Product prod){
        try{
            DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
            Connection conn = myFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("insert into cart(productID,name) values (?,?)");
            pstmt.setInt(1, prod.getId());
            pstmt.setString(2, prod.getName());
            pstmt.executeUpdate();
            conn.close();
        
        }catch(SQLException ex){
            
        }
    }
    @Override
    public void deleteFromCart(Product prod){
        try{
            DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
            Connection conn = myFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("delete from cart(productID,name) where productID = ? name = ?");
            pstmt.setInt(1, prod.getId());
            pstmt.setString(2, prod.getName());
            pstmt.executeUpdate();
            conn.close();
        
        }catch(SQLException ex){
            
        }
    
    }
    @Override
    public List<Product> getCart(Product prod){
        try {
            // TODO code application logic here
            DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
            Connection conn = myFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("select * from products where name like ?");
            pstmt.setString(1, "%"+prod.getName()+"%");
            ResultSet rs = pstmt.executeQuery();
            List<Product> result = new ArrayList<Product>();
            while (rs.next()){
                
                
                result.add(prod);
            }
            conn.close();
            return result;
        } catch (SQLException ex) {
            
        }
        return null;
    
    }
}
